package com.example.myapplication.constants;


import com.example.myapplication.BuildConfig;
import com.example.myapplication.MyApplication;
import com.example.myapplication.utils.PreferencesUtils;

/**
 * Describe : 与环境有关
 */
public class EnvironmentConstants {
    public static final String[] ENVIRONMENT_NAMES = {
            "正式环境", "测试环境"
    };
    private static final String TABLE_ENVIRONMENT_NAME = "table_app";


    public static final String KEY_ENVIRONMENT_INDEX = "key_environment_index";

    public final static String TEST_BASE_URL = "https://i.news.qq.com/";

    public final static String ONLINE_BASE_URL = "https://i.news.qq.com/";

    public static String BASE_URL = ONLINE_BASE_URL;


    /**
     * 切换环境
     *
     * @param index
     */
    public static void switchEnvironment(int index) {
        if (index == 0) {//正式环境
            BASE_URL = ONLINE_BASE_URL;
        } else if (index == 1) {//测试环境
            BASE_URL = TEST_BASE_URL;
        }
        PreferencesUtils.getInstance(TABLE_ENVIRONMENT_NAME, MyApplication.application).put(KEY_ENVIRONMENT_INDEX, index);
    }


    /**
     * 切换到到当前环境
     */
    public static void switchToCurrentEnvironment() {
        if (isDebug()) {
            int index =  PreferencesUtils.getInstance(TABLE_ENVIRONMENT_NAME, MyApplication.application).getInt(KEY_ENVIRONMENT_INDEX);
            switchEnvironment(index);
        }
    }

    /**
     * 是否是debug环境
     *
     * @return
     */
    public static boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    public static boolean isBaseUrl(String url) {
        if (url != null) {
            return url.contains(TEST_BASE_URL) || url.contains(ONLINE_BASE_URL);
        }
        return false;
    }


}
